//
//  ForgetPwdViewController.m
//  Ev_Master
//
//  Created by Evan on 2019/7/7.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "taskUserService.h"
#import "LoginSuccessViewController.h"

@interface ForgetPwdViewController ()
@property(nonatomic, assign) NSInteger currentCountDown;
@property(nonatomic, strong) NSTimer *timer;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    self.isNeedLogin = NO;
    [super viewWillAppear:animated];
    [self setHiddenNavigation:NO];
    [self hideHeadView:NO];
//    [self setStatusBarBackgroundColor:[UIColor clearColor]];
}

#pragma mark - 修改密码

- (IBAction)btnConfirm:(id)sender {
    if ([self.txtPhone.text  isEqualToString:@""] ||
        [self.txtPwd.text  isEqualToString:@""] || [self.txtCode.text  isEqualToString:@""]
        ) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的验证信息"];
        return;
    }
    
    if (![self.txtPwdQ.text isEqualToString: self.txtPwd.text]) {
        [DKProgressHUD showErrorWithStatus:@"两次密码不一致"];
        return;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:self.txtCode.text forKey:@"verifyCode"];
    
    [DKProgressHUD showLoading];
    [taskUserService getPwdToken:dict success:^(modelPwdTokenData * _Nonnull response, BOOL success) {
        [self postForgetPwd:response.data.password_reset_token];
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (void)postForgetPwd:(NSString*)pwdToken{
    if ([pwdToken isEqualToString:@""]) {
        [DKProgressHUD showErrorWithStatus:@"获取重置令牌失败"];
        return;
    }
    
     [[NetworkManager sharedManager].requestSerializer setValue:pwdToken forHTTPHeaderField:@"resettoken"];
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"username"];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:self.txtPwd.text forKey:@"password"];
    [dict setValue:self.txtCode.text forKey:@"verifyCode"];
    
    [taskUserService userForgetPwd:dict success:^(modelUserData * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        
        [DKProgressHUD showSuccessWithStatus:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
//        LoginSuccessViewController *vc = [[LoginSuccessViewController alloc]initWithNibName:@"LoginSuccessViewController" bundle:nil];
//        vc.labelTitle.text = @"恭喜你，密码设置成功";
//        [self.navigationController pushViewController:vc animated:YES];
        
//        [[NetworkManager sharedManager].requestSerializer removeObserver:pwdToken forKeyPath:@"resettoken"];
        
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        //注册失败
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


#pragma mark - 发送短信

- (IBAction)btnSendSMSClick:(UIButton *)sender {
    if ([self.txtPhone.text  isEqualToString:@""]) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    [DKProgressHUD showLoading];
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.txtPhone.text forKey:@"mobile"];
    [dict setValue:@"用户登录密码修改" forKey:@"usage"];
    [taskUserService sendSMS:dict success:^(BaseModel * _Nonnull response, BOOL success) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showSuccessWithStatus:response.message];
        
        [self startCount];
        
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}




#pragma mark - 倒计时
- (void)startCount
{
    /**
     *  添加定时器
     */
    self.currentCountDown = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.timer fire];
}

- (void)countDown{
    
    if (self.currentCountDown >0) {
        //设置界面的按钮显示 根据自己需求设置
        [self.btnSendSMS setTitle:[NSString stringWithFormat:@"%ld重新获取",(long)self.currentCountDown] forState:UIControlStateNormal];
        self.btnSendSMS.enabled = NO;
        self.currentCountDown -= 1;
    }else{
        [self removeTimer];
        self.btnSendSMS.enabled = YES;
    }
    
}

/**
 *  移除定时器
 */
- (void)removeTimer
{
    self.currentCountDown = 0;
    [self setCaptchaEnable:YES];
    [self.timer invalidate];
    //    self.timer = nil;
}


//因为iOS 7下 按钮 enabled= NO, 不能设置文字
#pragma mark - 设置按钮状态
- (void)setCaptchaEnable:(BOOL)enabled{
    //可以按
    if (enabled) {
        self.btnSendSMS.userInteractionEnabled = YES;
        [self.btnSendSMS setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }else{
        self.btnSendSMS.userInteractionEnabled = NO;
    }
}



@end
